//
//  HEHomeNextCell.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/5/13.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEHomeNextCell.h"
#import "HEHomeServerItemCell.h"

@interface HEHomeNextCell ()<UITableViewDelegate, UITableViewDataSource>
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) MASConstraint *tableHeightLayout;

@end

@implementation HEHomeNextCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.contentView.backgroundColor = CViewBgColor;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    UIView *warp = [UIView new];
    warp.backgroundColor = KWhiteColor;
    warp.layer.cornerRadius = 10;
    warp.layer.masksToBounds = YES;
    [self.contentView addSubview:warp];
    
    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(0);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
    }];
    
    UILabel *titleLb = [UILabel new];
    titleLb.font = DYBlodFont(20);
    titleLb.text = @"最近服务";
    [warp addSubview: titleLb];
    
    
    UILabel *serverNameLb = [UILabel new];
    serverNameLb.text = @"服务项目";
    serverNameLb.font = DYBlodFont(17);
    serverNameLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:serverNameLb];
    
    UILabel *serverDateLb = [UILabel new];
    serverDateLb.text = @"服务日期";
    serverDateLb.font = DYBlodFont(17);
    serverDateLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:serverDateLb];
    
    UIView *line = [UIView new];
    line.backgroundColor = CSplitLineColor;
    [warp addSubview:line];
    
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).offset(15);
        make.left.equalTo(warp).offset(15);
        make.right.equalTo(warp).offset(-15);
    }];

 
    NSArray *arr = @[serverNameLb, serverDateLb];
    [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    [arr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLb.mas_bottom).offset(15);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(serverNameLb);
        make.centerX.equalTo(warp);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(20);
    }];
    
    
    [warp addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(serverNameLb.mas_bottom).mas_offset(10);
        make.left.equalTo(warp);
        make.right.equalTo(warp);
        make.height.mas_equalTo(50).priorityLow();
        make.bottom.equalTo(warp);
    }];
}
 
- (void)setDataSource:(NSMutableArray<HESickedPlanModel *> *)dataSource {
    _dataSource = dataSource;
    [self.tableView reloadData];
    [_tableView layoutIfNeeded];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        self.tableHeightLayout = make.height.mas_equalTo(self.tableView.contentSize.height).priorityLow();
    }];
}

- (void)configCellWithFiterModel:(GDFiterCellModel *)fiterModel indexPath:(NSIndexPath *)indexPath {
    self.dataSource = fiterModel.cellData;
}

#pragma mark -
- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight + 50 + 50, KScreenWidth, KScreenHeight - ZXNavBarHeight -50 -50) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollsToTop = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
         
        [_tableView registerClass:[HEHomeServerItemCell class] forCellReuseIdentifier: @"HEHomeServerItemCell"];
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEHomeServerItemCell *cell = [_tableView dequeueReusableCellWithIdentifier: @"HEHomeServerItemCell"];
    [cell configCellWithModel:_dataSource[indexPath.row] indexPath:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: @"HEHomeServerItemCell" cacheByIndexPath:indexPath configuration:^(HEHomeServerItemCell *cell) {
        [cell configCellWithModel:self.dataSource[indexPath.row] indexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


@end
